function []= plot_LongTermGrade(grade,gradeYr,RWQname) 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BAR PLOT OF THE RWQ 5-YR 95TH HAZEN PERCENTILE ANNUAL GRADES  WITH 
% COLOR CODED BAR ACCORDING TO GRADE 
% 
% INPUT     : - grade : array of 5-yr 95th hazen percentile annual grade
%             - greadeYr: array of corresponding year (as of start of season)
%             - RWQname : name of site (string)
%
% EXAMPLE   : load TestData.mat
%             load Bays_n_Beaches.mat
%             plot_LongTermGrade(testdata.grade5yr,...
%             testdata.grade5yr_y,BEACHES{1}) 
%
% Author: Benedicte Pasquer, IMOS/eMII (http://imos.org.au/)
% email: benedicte.pasquer@utas.edu.au
% May 2013   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin~=3
    error('Not enough input arguments')
end
if ~isnumeric(gradeYr) 
    error('date must be in Matlab format')
end
if ~ischar(RWQname),error('RWQname must be a string'),end
   

figure('position',[1 1 900 550],'Color',[.9 .98 .98])


% BAR PLOT
bar_g =  bar(grade);
bar_child = get(bar_g,'Children');
set(bar_child,'CDataMapping','direct');

% BAR COLOR CODED ACCORDING TO GRADE LEVEL
mycolor = [0 1 0; 1 1 0; 1 0 0]; 
greenRange = 1*(grade<200);
redRange = 3*(grade>=500);
yellowRange = 2*~(greenRange+redRange);
index = greenRange + redRange + yellowRange;
set(bar_child, 'CData',index,'Edgecolor','none');
colormap(mycolor);

% TITLE AND LABEL
ymax = max(grade)*1.25;
xmax = length(gradeYr);
Yr_TickLabel = strcat(num2str((gradeYr(:)-1)),'-',num2str((gradeYr(:))));
title({'Water quality over past summers',['at ', RWQname],[]},...
    'Fontname','arial',...
    'Fontsize',14,'Fontweight','bold',...
    'Color', [0 0 .7]);
ylabel({'5-year 95^t^h Hazen percentile';'for Enterococci [Enterococci/100ml]'},...
    'Fontname','arial',...
    'Fontsize',14,'Fontweight','bold',...
    'Color', [0 0 .7]);

set (gca,'Ylim',[0 ymax],'XLim', [0 xmax+1],...
    'Xtick',1:length(gradeYr),...
    'XtickLabel',Yr_TickLabel,'Fontname','arial',...
    'Fontsize',14,'Fontweight','bold',...
    'Color', [.9 .98 .98],'YColor', [0 0 .7], 'Xcolor', [0 0 .7],...
    'Position',[0.13 0.25 0.72 0.61],'Visible','on')

hh = rotateXLabels(gca(),45);
set (hh,'Color',[0 0 .7])

% CREATE A SECOND AXIS FOR LEGEND DISPLAY  
ax2 = axes('Position',[0.85 0.30 0.12 0.61],...
'XAxisLocation','top',...
'YAxisLocation','left',...
'Color','none',...
'XColor','k','YColor','k',...
'YLim',[0 1],'Xlim',[0 .2],...
'Visible','off');

hr1 = rectangle('Position',[.035 .86  .07 .05],'Facecolor','g');
hr2 = rectangle('Position',[.035 .80  .07 .05],'Facecolor','y');
hr3 = rectangle('Position',[.035 .74  .07 .05],'Facecolor','r');  
set([hr1 hr2 hr3],'Edgecolor','k','Parent',ax2)
ht1 = text(.13,.89,'GOOD');
ht2 = text(.13,.83,'FAIR');
ht3 = text(.13,.78,'POOR');
set([ht1 ht2 ht3],'Color',[0 0 .7], 'Fontname','arial','Fontsize',14,'Fontweight','bold')

set(gcf, 'InvertHardCopy', 'off');
figname = RWQname;
figname(isspace(figname)) = []; %REMOVE BLANK FROM FILENAME
saveas(gcf,figname,'png')


